/**
 * Copyright(c) 2013 Jade Techonologies Co., Ltd.
 *
 * History:
 *   13-7-11 上午10:33 Created by dwzhang
 */
package com.jade.framework.base.util;

import org.apache.commons.lang3.StringUtils;

/**
 * 字符串工具类的扩展。
 *
 * @author dwzhang
 * @version 1.0 13-7-11 上午10:33
 */
public class StringUtilsEx extends StringUtils
{
    public static final String NULL = "null";
    public static final char[] NULL_CHARS = NULL.toCharArray();
    /** 空的字符串数组 */
    public static final String[] EMPTY_STRING_ARRAY = new String[0];

    /** 空的字符串 */
    public static final String EMPTY_STRING = "";

    public static final byte[] toBytes(String str)
    {
        return toBytes(str, null);
    }

    public static final byte[] toBytes(String str, String encoding)
    {
        byte[] bytes = null;
        if (str == null) {
            return null;
        }
        else if (str.length() == 0) {
            return DataUtils.EMPTY_BYTE_ARRAY;
        }
        else if (encoding == null) {
            bytes = str.getBytes();
        }
        else {
            try {
                bytes = str.getBytes(encoding);
            }
            catch (Exception e) {
                bytes = str.getBytes();
            }
        }
        return bytes;
    }

    public static final String toString(byte[] bytes, int off, int len, String encoding)
    {
        String result = null;
        if (bytes == null) {
            return null;
        }
        else if (encoding == null) {
            result = new String(bytes, off, len);
        }
        else {
            try {
                result = new String(bytes, off, len, encoding);
            }
            catch (Exception e) {
                result = new String(bytes, off, len);
            }
        }
        return result;
    }

    public static final String toString(byte[] bytes, String encoding)
    {
        if (bytes == null) {
            return null;
        }
        return toString(bytes, 0, bytes.length, encoding);
    }

    public static final String toString(byte[] bytes)
    {
        return toString(bytes, null);
    }
}
